﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApp.BaseDataSvc;
using System.Configuration;
using YY.Common;
using System.Web.Script.Serialization;
using System.Collections;

namespace WebApp
{
    public partial class EditOrderFrm : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(EditOrderFrm), this);
            //var orderId = Request.Params["OrderId"];
            //if (string.IsNullOrEmpty(orderId))
            //{
            //    Response.Write("非法进入");
            //    Response.End();
            //}
        }

        [AjaxPro.AjaxMethod]
        public TB_User GetCurrentUser()
        {
            try
            {
                return Session["User"] as TB_User;
            }
            catch { }
            return null;
        }


        [AjaxPro.AjaxMethod]
        public AjaxResult<int> SaveBase(string id, string eOrderDate, string eBizMan, string eAgent, string eClient, string eInvoiceHeader, string eContactInfo,
            string eCCode, string eStartPort, string eEndPort, string eImptAndExpt, string eCustomsDeadline, string eStowageMethod, string eMemo, bool? eHasTrailer,
            bool? eHasInvoice, bool? eIsCustoms, bool? eIsOcean)
        {
            bool update = !string.IsNullOrEmpty(id);
            AjaxResult<int> result = new AjaxResult<int>();
            result.Data = 0;
            result.Msg = null;
            try
            {
                if (!this.IsPermitOP(EnumCommon.PermissionOpType.Save, this.GetType().Name))
                {
                    result.Msg = "没有操作权限！";
                    return result;
                }

                TB_User user = (Session["User"] as TB_User);
                int? orderId = null;
                if (update)
                    orderId = Convert.ToInt32(id);
                var dupl = this.BaseDataProxy.ExistsOrderCode(orderId, null, eCCode);
                if (dupl != null)
                {
                    if (dupl)
                    {
                        result.Msg = "此单号已存在！";
                        return result;
                    }
                }


                TB_Order order = new TB_Order();
                if (update)
                {
                    order = this.BaseDataProxy.GetOrder(Convert.ToInt32(id));
                    if (order.isSubmit)
                    {
                        result.Msg = "已提交财务审核单据不能修改！";
                        return result;
                    }
                }

                WebCommon.EvalControlValueToData(order, "cCode", eCCode);
                WebCommon.EvalControlValueToData(order, "client", eClient);
                WebCommon.EvalControlValueToData(order, "contactInfo", eContactInfo);
                WebCommon.EvalControlValueToData(order, "customsDeadline", eCustomsDeadline);
                WebCommon.EvalControlValueToData(order, "hasInvoice", eHasInvoice);
                WebCommon.EvalControlValueToData(order, "hasTrailer", eHasTrailer);
                WebCommon.EvalControlValueToData(order, "imptAndExpt", eImptAndExpt);
                WebCommon.EvalControlValueToData(order, "invoiceHeader", eInvoiceHeader);
                order.isClosed = false;
                WebCommon.EvalControlValueToData(order, "isCustoms", eIsCustoms);
                WebCommon.EvalControlValueToData(order, "isOcean", eIsOcean);
                WebCommon.EvalControlValueToData(order, "memo", eMemo);
                WebCommon.EvalControlValueToData(order, "operator", eBizMan);
                WebCommon.EvalControlValueToData(order, "orderTime", eOrderDate);
                WebCommon.EvalControlValueToData(order, "startPort", eStartPort);
                WebCommon.EvalControlValueToData(order, "endPort", eEndPort);
                WebCommon.EvalControlValueToData(order, "stowageMethod", eStowageMethod);

                if (update)
                {
                    this.BaseDataProxy.UpdateOrder(order);
                    result.Data = order.id;
                }
                else
                {
                    order.agent = user.id;
                    string header = ConfigurationManager.AppSettings["OrderCodeHeader"];
                    int len = Convert.ToInt32(ConfigurationManager.AppSettings["SerialLen"]);
                    order.code = this.BaseDataProxy.GetSerialNo(1, header, len, true, true, true);
                    order.createTime = DateTime.Now;
                    //order.status = EnumCommon.OrderStatus.订舱录入.ToString();
                    int newId = this.BaseDataProxy.AddOrder(order);
                    result.Data = newId;
                }
            }
            catch (Exception ex)
            {
                result.Data = 0;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> SaveTC(int orderId, string agent, string deadline, string address, bool isPonderation, string memo, string opContact, string charge, string returnPort, string specs, string stowageArea, string stowageContact)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;

            try
            {
                if (!this.IsPermitOP(EnumCommon.PermissionOpType.Save, this.GetType().Name))
                {
                    result.Msg = "没有操作权限！";
                    return result;
                }

                var order = this.BaseDataProxy.GetOrder(orderId);
                if (order.isSubmit)
                {
                    result.Msg = "已提交财务审核单据不能修改！";
                    return result;
                }

                TB_Trailer trailer = new TB_Trailer();
                WebCommon.EvalControlValueToData(trailer, "address", address);
                WebCommon.EvalControlValueToData(trailer, "agent", agent);
                WebCommon.EvalControlValueToData(trailer, "deadline", deadline);
                WebCommon.EvalControlValueToData(trailer, "isPonderation", isPonderation);
                WebCommon.EvalControlValueToData(trailer, "memo", memo);
                WebCommon.EvalControlValueToData(trailer, "opContact", opContact);
                WebCommon.EvalControlValueToData(trailer, "charge", charge);
                WebCommon.EvalControlValueToData(trailer, "returnPort", returnPort);
                WebCommon.EvalControlValueToData(trailer, "specs", specs);
                WebCommon.EvalControlValueToData(trailer, "stowageArea", stowageArea);
                WebCommon.EvalControlValueToData(trailer, "stowageContact", stowageContact);

                result.Data = this.BaseDataProxy.UpdateTrailer(orderId, new TB_Trailer[] { trailer });

            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;


        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<TB_Trailer> GetTC(int orderId)
        {
            AjaxResult<TB_Trailer> result = new AjaxResult<TB_Trailer>();
            result.Data = null;
            result.Msg = null;
            try
            {
                result.Data = this.BaseDataProxy.GetTrailer(orderId);
            }
            catch (Exception ex)
            {
                result.Data = null;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> ClearTC(int orderId)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;
            try
            {
                if (!this.IsPermitOP(EnumCommon.PermissionOpType.Delete, this.GetType().Name))
                {
                    result.Msg = "没有操作权限！";
                    return result;
                }

                var order = this.BaseDataProxy.GetOrder(orderId);
                if (order.isSubmit)
                {
                    result.Msg = "已提交财务审核单据不能修改！";
                    return result;
                }

                result.Data = this.BaseDataProxy.UpdateTrailer(orderId, null);
            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> CancelOrder(int id)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;
            try
            {
                if (!this.IsPermitOP(EnumCommon.PermissionOpType.Delete, this.GetType().Name))
                {
                    result.Msg = "没有操作权限！";
                    return result;
                }

                MsgBoolean mb = this.BaseDataProxy.DeleteOrder(id);
                result.Data = mb.Ok;
                result.Msg = mb.Msg;
            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<TB_Order> GetOrderBase(int id)
        {
            AjaxResult<TB_Order> result = new AjaxResult<TB_Order>();
            result.Data = null;
            result.Msg = null;
            try
            {
                result.Data = this.BaseDataProxy.GetOrder(id);
            }
            catch (Exception ex)
            {
                result.Data = null;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> SaveBG(string flag,int orderId, bool isCheck, bool isDrawback, bool isKdsk, bool isNotice, bool isOffer, bool isPass, string memo, string notifier, string receiver,
            string runNum, string status, string stockholder, string transferType, string address, string agent, string applyDate, string boatCorp, string boatName, string bookingMemo,
            string passTime, string contactMan, string contactTel, string demand, string destination, string endPort, string checkOff)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;

            try
            {
                if (flag == "ycts")
                {
                    if (!this.IsPermitOP(EnumCommon.PermissionOpType.BackTax, this.GetType().Name))
                    {
                        result.Msg = "没有操作权限！";
                        return result;
                    }
                }

                if (!this.IsPermitOP(EnumCommon.PermissionOpType.Save, this.GetType().Name))
                {
                    result.Msg = "没有操作权限！";
                    return result;
                }

                var order = this.BaseDataProxy.GetOrder(orderId);
                if (order.isSubmit)
                {
                    result.Msg = "已提交财务审核单据不能修改！";
                    return result;
                }

                TB_Customs customs = new TB_Customs();
                WebCommon.EvalControlValueToData(customs, "isCheck", isCheck);
                WebCommon.EvalControlValueToData(customs, "isDrawback", isDrawback);
                WebCommon.EvalControlValueToData(customs, "isKdsk", isKdsk);
                WebCommon.EvalControlValueToData(customs, "isNotice", isNotice);
                WebCommon.EvalControlValueToData(customs, "isOffer", isOffer);
                WebCommon.EvalControlValueToData(customs, "isPass", isPass);
                WebCommon.EvalControlValueToData(customs, "memo", memo);
                WebCommon.EvalControlValueToData(customs, "notifier", notifier);
                WebCommon.EvalControlValueToData(customs, "receiver", receiver);
                WebCommon.EvalControlValueToData(customs, "runNum", runNum);
                WebCommon.EvalControlValueToData(customs, "status", status);
                WebCommon.EvalControlValueToData(customs, "stockholder", stockholder);
                WebCommon.EvalControlValueToData(customs, "transferType", transferType);
                WebCommon.EvalControlValueToData(customs, "address", address);
                WebCommon.EvalControlValueToData(customs, "agent", agent);
                WebCommon.EvalControlValueToData(customs, "applyDate", applyDate);
                WebCommon.EvalControlValueToData(customs, "boatCorp", boatCorp);
                WebCommon.EvalControlValueToData(customs, "boatName", boatName);
                WebCommon.EvalControlValueToData(customs, "bookingMemo", bookingMemo);
                if (!string.IsNullOrEmpty(passTime))
                    WebCommon.EvalControlValueToData(customs, "passTime", passTime);
                WebCommon.EvalControlValueToData(customs, "contactMan", contactMan);
                WebCommon.EvalControlValueToData(customs, "contactTel", contactTel);
                WebCommon.EvalControlValueToData(customs, "demand", demand);
                WebCommon.EvalControlValueToData(customs, "destination", destination);
                WebCommon.EvalControlValueToData(customs, "endPort", endPort);
                WebCommon.EvalControlValueToData(customs, "contactMan", contactMan);
                //IEnumerable<TB_CheckOff> cfs = WebCommon.JsonDeserialize<IEnumerable<TB_CheckOff>>(checkOff);
                //IEnumerable<TB_CheckOff> cfs = JsonHelper.JsonDeserialize<IEnumerable<TB_CheckOff>>(checkOff);                
                checkOff = checkOff.Replace("\"ExtensionData\":{},", "");
                IEnumerable<TB_CheckOff> cfs = new JavaScriptSerializer().Deserialize<IEnumerable<TB_CheckOff>>(checkOff);
                //var ok = wfs.Any();
                if (cfs != null & cfs.Any())
                    customs.TB_CheckOff = cfs.ToArray();
                result.Data = this.BaseDataProxy.UpdateCustoms(orderId, new TB_Customs[] { customs });

            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<TB_Customs> GetBG(int orderId)
        {

            AjaxResult<TB_Customs> result = new AjaxResult<TB_Customs>();
            result.Data = null;
            result.Msg = null;
            try
            {
                result.Data = this.BaseDataProxy.GetCustoms(orderId);
            }
            catch (Exception ex)
            {
                result.Data = null;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> ClearBG(int orderId)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;
            try
            {
                if (!this.IsPermitOP(EnumCommon.PermissionOpType.Delete, this.GetType().Name))
                {
                    result.Msg = "没有操作权限！";
                    return result;
                }

                var order = this.BaseDataProxy.GetOrder(orderId);
                if (order.isSubmit)
                {
                    result.Msg = "已提交财务审核单据不能修改！";
                    return result;
                }

                result.Data = this.BaseDataProxy.UpdateCustoms(orderId, null);
            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> BGBackTax(int orderId)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;
            try
            {
                if (!this.IsPermitOP(EnumCommon.PermissionOpType.BackTax, this.GetType().Name))
                {
                    result.Msg = "没有操作权限！";
                    return result;
                }

                var model = this.BaseDataProxy.GetCustoms(orderId);
                if (model != null)
                {
                    model.status = EnumCommon.CustomsStatus.已出退税.ToString();
                    TB_User user = Session["User"] as TB_User;
                    result.Data = this.BaseDataProxy.UpdateCustomsModel(model, user.role);
                }
            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> DeleteCase(int id, int orderId)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;
            try
            {
                if (!this.IsPermitOP(EnumCommon.PermissionOpType.Delete, this.GetType().Name))
                {
                    result.Msg = "没有操作权限！";
                    return result;
                }

                var order = this.BaseDataProxy.GetOrder(orderId);
                if (order.isSubmit)
                {
                    result.Msg = "已提交财务审核单据不能修改！";
                    return result;
                }

                result.Data = this.BaseDataProxy.DeleteCase(id);
            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> DeleteCargo(int id, int orderId)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;
            try
            {
                if (!this.IsPermitOP(EnumCommon.PermissionOpType.Delete, this.GetType().Name))
                {
                    result.Msg = "没有操作权限！";
                    return result;
                }

                var order = this.BaseDataProxy.GetOrder(orderId);
                if (order.isSubmit)
                {
                    result.Msg = "已提交财务审核单据不能修改！";
                    return result;
                }

                result.Data = this.BaseDataProxy.DeleteCargo(id);
            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> DeleteFile(int id, int orderId)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;
            try
            {
                if (!this.IsPermitOP(EnumCommon.PermissionOpType.Delete, this.GetType().Name))
                {
                    result.Msg = "没有操作权限！";
                    return result;
                }

                var order = this.BaseDataProxy.GetOrder(orderId);
                if (order.isSubmit)
                {
                    result.Msg = "已提交财务审核单据不能修改！";
                    return result;
                }

                result.Data = this.BaseDataProxy.DeleteAttach(id);
            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> DeleteFee(int[] ids, int orderId)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;
            try
            {
                if (!this.IsPermitOP(EnumCommon.PermissionOpType.Delete, this.GetType().Name))
                {
                    result.Msg = "没有操作权限！";
                    return result;
                }

                TB_User user = Session["User"] as TB_User;
                var order = this.BaseDataProxy.GetOrder(orderId);
                if (user.role == (int)EnumCommon.SysRoleType.财务员)
                {
                    if (order.isClosed)
                    {
                        result.Msg = "已收款结束单据不能删除！";
                        return result;
                    }
                }
                else
                {
                    if (order.isSubmit & user.role != (int)EnumCommon.SysRoleType.管理员)
                    {
                        result.Msg = "已提交财务审核单据不能删除！";
                        return result;
                    }
                }

                foreach (int i in ids)
                    result.Data = this.BaseDataProxy.DeleteFee(i);
            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> VerifyFee(int[] ids, int orderId, bool isVerify)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;
            try
            {
                if (!this.IsPermitOP(EnumCommon.PermissionOpType.Verify, this.GetType().Name))
                {
                    result.Msg = "没有操作权限！";
                    return result;
                }

                TB_User user = Session["User"] as TB_User;
                var order = this.BaseDataProxy.GetOrder(orderId);
                if (order.financier != user.id & user.role != (int)EnumCommon.SysRoleType.管理员)
                {
                    result.Msg = "对该单据没有审核权限！";
                    return result;
                }

                foreach (int i in ids)
                {
                    var fee = this.BaseDataProxy.GetFee(i);
                    fee.isVerify = isVerify;
                    fee.verifyMan = user.id;
                    fee.verifyTime = DateTime.Now;
                    result.Data = this.BaseDataProxy.UpdateFee(fee);
                }
            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }
    }
}
